package com.agileboot.domain.airport.inspection.query;

import cn.hutool.core.util.StrUtil;
import com.agileboot.common.core.page.AbstractPageQuery;
import com.agileboot.domain.airport.inspection.db.SirInspectionEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Arrays;
import java.util.Date;

/**
 * @author valarchie
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class SirInspectionQuery extends AbstractPageQuery<SirInspectionEntity> {

    private String inspectionName;
    private Long deptId;
    private Date startTime;
    private Date endTime;
    private String status;
    private String createBy;
    private String inspectionDeptId;
    private String inspectionType;




    @Override
    public QueryWrapper<SirInspectionEntity> addQueryCondition() {
        QueryWrapper<SirInspectionEntity> queryWrapper = new QueryWrapper<SirInspectionEntity>()
                // 模糊查询检查名称
                .like(StrUtil.isNotEmpty(inspectionName), "inspection_name", inspectionName)
                // 等值查询部门 ID
                .eq(deptId != null, "inspection_dept_id", deptId)

                .eq(inspectionType != null, "inspection_type", inspectionType)

                // 日期范围查询计划日期
                .ge(startTime != null, "plan_date", startTime)
                .le(endTime != null, "plan_date", endTime)
                // 模糊查询创建者
                .like(StrUtil.isNotEmpty(createBy), "create_by", createBy);

        // 处理 status 字段
        if (StrUtil.isNotEmpty(status)) {
            String[] statusArray = status.split(",");
            if (statusArray.length == 1) {
                queryWrapper.eq("status", statusArray[0]);
            } else {
                queryWrapper.in("status", Arrays.asList(statusArray));
            }
        }

        // 当前端没有选择排序字段时，则使用 create_time 字段升序排序（在父类 AbstractQuery 中默认为升序）
        if (StrUtil.isEmpty(this.getOrderColumn())) {
            this.setOrderColumn("create_time");
        }
        this.setTimeRangeColumn("create_time");

        return queryWrapper;
    }
}